package com.sgcc.pda.sdk.utils.db;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.sgcc.pda.baselibrary.ConstPath;
import com.sgcc.pda.baselibrary.thread.FixedThreadUtil;
import com.sgcc.pda.greendao.entity.AppBaseInfo;
import com.sgcc.pda.sdk.dao.ContentProviderDao;
import com.sgcc.pda.sdk.dao.impl.ContentProviderDaoImpl;
import com.sgcc.pda.sdk.entity.BehaviorTrajectoryInfo;
import com.sgcc.pda.sdk.entity.DataBean;
import com.sgcc.pda.sdk.entity.MsgOfflineInfo;
import com.sgcc.pda.sdk.entity.TaskUpInfo;
import com.sgcc.pda.sdk.entity.TrafficStatsInfo;
import com.sgcc.pda.sdk.entity.table.AppApkInfo;
import com.sgcc.pda.sdk.entity.table.BaseTaskInfo;
import com.sgcc.pda.sdk.entity.table.FileInfo;
import com.sgcc.pda.sdk.entity.table.RegisterInfo;
import com.sgcc.pda.sdk.entity.table.SharepreferenceInfo;
import com.sgcc.pda.sdk.entity.table.UserInfo;
import com.sgcc.pda.sdk.utils.ObjectSerializableUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 内容提供者工具类.
 *
 * @author 王沛栋
 * @data 2018/1/19 10:43
 */

public class ContentProviderUtil {
    private static ContentProviderDao contentProviderDao;

    private static ContentProviderDao getContentProviderDao() {
        if (contentProviderDao == null) {
            contentProviderDao = new ContentProviderDaoImpl();
        }
        return contentProviderDao;
    }

    public static void setSharepreference(final Context context, final SharepreferenceInfo info) {
        getContentProviderDao().setSharepreference(context, info);
    }

    /**
     * 获取存储在Data应用中sp的String值
     *
     * @param context
     * @param key
     * @return
     */
    public static String getSharepreferenceValue(Context context, String editKey, String key) {
        return getContentProviderDao().getSharepreferenceValue(context, editKey, key);
    }

    /**
     * 获取存储在Data应用中sp的boolean值
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean getSharepreferenceBooleanValue(Context context, String editKey, String key) {
        return getContentProviderDao().getSharepreferenceBooleanValue(context, editKey, key);
    }



    /**
     * 获取应用分类
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getAppTypeList(Context context) {
        return getContentProviderDao().getAppTypeList(context);
    }

    /**
     * 获取应用列表
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getAppBaseInfoList(Context context) {
        return getContentProviderDao().getAppBaseInfoList(context);
    }

    /**
     * 获取增量应用列表
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getAppAddUpDataBaseInfoList(Context context) {
        return getContentProviderDao().getAppBaseInfoList(context);
    }

    /**
     * 获取应用列表通过关键字获取应用包名查询
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getAppBaseInfoListBySearchAppName(Context context, String key) {
        return getContentProviderDao().getAppBaseInfoListBySearchAppName(context, key);
    }

    /**
     * 更新app的增量更新状态
     */
    public static void setAppAddUpdateState(final Context context, final String packageName, final String appAddUpdate) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveAddOrUpdateAppBaseInfoState(context, packageName, appAddUpdate);

            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 查新需要合并补丁包的APP基础信息
     *
     * @param context   上下文
     * @param addUpdate 更新状态
     */
    public static List<AppApkInfo> getAppBaseInfoByAddUpdate(Context context, String addUpdate) {
        return getContentProviderDao().selectAppBaseInfoByAddUpdate(context, addUpdate);
    }


    /**
     * 获取文件实体
     *
     * @param context
     * @param fileNo
     * @return
     */
    public static FileInfo getFileInfo(Context context, String fileNo) {
        return getContentProviderDao().getFileInfo(context, fileNo);
    }

    /**
     * 保存文件信息
     *
     * @param context
     * @param info
     */
    public static void saveFileInfo(final Context context, final FileInfo info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveFileInfo(context, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);

    }


    /**
     * 保存注册信息
     *
     * @param context
     * @param info
     */
    public static void saveRegisterInfo(final Context context, final RegisterInfo info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveRegisterInfo(context, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);

    }


    /**
     * 删除注册信息
     *
     * @param context
     * @return
     */
    public static boolean deleteRegisterInfo(Context context) {
        return getContentProviderDao().deleteRegisterInfo(context);
    }

    /**
     * 获取注册信息
     *
     * @param context
     * @return
     */
    public static RegisterInfo getRegisterInfo(Context context) {
        return getContentProviderDao().getRegisterInfo(context);
    }


    /**
     * 保存掌机使用人列表
     *
     * @param context
     * @param str
     */
    public static void saveUserInfoList(final Context context, final String str) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveUserInfoList(context, str);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 根据关键字搜索联系人
     *
     * @param context
     * @param key
     * @return
     */
    public static List<UserInfo.User> getUserInfoList(Context context, String key) {
        return getContentProviderDao().getUserInfoList(context, key);
    }


    /**
     * 删除掌机联系人
     *
     * @param context
     * @return
     */
    public static boolean deleteUserInfoList(Context context) {
        return getContentProviderDao().deleteUserInfoList(context);
    }

    public static void saveMarketDesktopInfo(Context context, List<AppBaseInfo> list) {
        getContentProviderDao().saveMarketDesktopInfo(context, list);
    }

    public static void deletMarketDesktopInfo(Context context, List<AppBaseInfo> list) {
        getContentProviderDao().saveMarketDesktopInfo(context, list);
    }

    /**
     * 获取最近使用APP列表
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getMarketTopInfo(Context context) {
        return getContentProviderDao().getMarketTopInfo(context);
    }

    /**
     * 获取分类桌面
     *
     * @param context
     * @return
     */
    public static List<AppBaseInfo> getSortMarketTopInfo(Context context, String dId) {
        return getContentProviderDao().getSortMarketTopInfo(context, dId);
    }


    public static boolean updateAppBaseInfoApkState(Context context,
                                                    String packageName, int state) {
        return getContentProviderDao().updateAppBaseInfoApkState(context, packageName, state);
    }

    /**
     * 更新应用apk下载状态
     *
     * @param context
     * @param packageName
     * @param state       0默认 1正在下载 2下载成功 3下载失败
     * @return
     */
    public static boolean saveOrUpdateAppApkInfoState(Context context, String packageName, int state, String progress) {
        return getContentProviderDao().saveOrUpdateAppApkInfoState(context, packageName, state, progress);
    }

    /**
     * 更新应用apk下载状态
     *
     * @param context
     * @param packageName
     * @param state       0默认 1正在下载 2下载成功 3下载失败
     * @return
     */
    public static boolean saveOrUpdateAppApkInfoState(Context context, String packageName, int state, String progress, String appName) {
        return getContentProviderDao().saveOrUpdateAppApkInfoState(context, packageName, state, progress, appName);
    }

    /**
     * 获取应用状态
     *
     * @param context
     * @param packageName
     * @return
     */
    public static AppApkInfo getAppApkInfoByPackageName(Context context, String packageName) {
        return getContentProviderDao().getAppApkInfoByPackageName(context, packageName);
    }

    /**
     * 删除应用信息
     *
     * @param context
     * @return
     */
    public static boolean deletAppBaseInfo(Context context) {
        return getContentProviderDao().deletAppBaseInfo(context);
    }

    /**
     * 根据工单编号删除工单
     *
     * @param context
     * @param appNo
     * @return
     */
    public static boolean deleteBaseTaskInfo(Context context, String appNo) {
        return getContentProviderDao().deletAppBaseInfoByAppNo(context, appNo);
    }

    public static void saveOrUpdateBaseTaskInfo(final Context context, final List<BaseTaskInfo> list) {
        getContentProviderDao().saveOrUpdateBaseTaskInfo(context, list);
    }

    /**
     * 获取基础工单信息列表
     *
     * @param context    上下文
     * @param selectArgs 获取全部工单{"0"}
     *                   获取逾期工单{"1","当前时间戳"}
     *                   获取紧急工单{"2","当前时间戳"}
     *                   获取一般工单{"3","当前时间戳"}
     *                   根据资产编号获取工单{"6", "资产编号"}
     *                   根据工单类型获取工单{"10", "工单类型编号"}
     *                   根据工单类型获取工单{"12", "工单类型编号"...}
     */
    public static List<BaseTaskInfo> getBaseTaskInfoList(Context context, String[] selectArgs) {
        return getContentProviderDao().getBaseTaskInfoList(context, selectArgs);
    }

    /**
     * 按工单类型查询待办工单列表
     *
     * @param args 工单类型编号 {"工单类型编号"...}
     */
    @NonNull
    public static List<BaseTaskInfo> getTaskListByType(@NonNull Context context, @NonNull String[] args) {
        // 查询条件
        String[] selectArgs = new String[args.length + 1];
        // 12是根据工单类型进行批量查询待办工单
        selectArgs[0] = "12";
        // 工单类型编号
        System.arraycopy(args, 0, selectArgs, 1, args.length);

        // 按工单类型查询本地待处理工单
        List<BaseTaskInfo> list = getContentProviderDao().getBaseTaskInfoList(context, selectArgs);

        // 根据待上传文件目录读取本地待上传工单数
        List<String> fileNames = ObjectSerializableUtil.getTaskNames(ConstPath.SITE_WORK_PATH);
        if (fileNames != null) {
            // 如果待处理的工单已转为待处理的状态，则不展示在待处理中
            for (String name : fileNames) {
                // 文件名称就是工单编号
                name = name.substring(0, name.indexOf("."));
                // 遍历列表，移除待上传工单
                for (BaseTaskInfo info : list) {
                    if (TextUtils.equals(info.getAppNo(), name)) {
                        list.remove(info);
                        break;
                    }
                }
            }
        }
        return list;
    }

    /**
     * 获取子应用工单个数
     */
    public static int getSonTaskNumInfo(Context context, String[] selectArgs) {
        return getContentProviderDao().getSonTaskNumInfo(context, selectArgs);
    }

    /**
     * 保存用户安装和卸载的状态
     *
     * @param context
     * @param list
     * @param state   0安装 1卸载
     */
    public static void saveOrUpdateAppInstallInfoList(final Context context, final List<AppBaseInfo> list, final int state) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveOrUpdateAppInstallInfoList(context, list, state);
            }
        };
        FixedThreadUtil.executeThread(runnable);

    }


    /**
     * 获取App安装信息
     *
     * @param context
     */
    public static List<AppBaseInfo> getAppInstallInfoList(Context context) {
        return getContentProviderDao().getAppInstallInfoList(context);
    }

    /**
     * 获取app信息列表
     *
     * @param context
     */
    public static AppBaseInfo getAppBaseInfoByPackageName(Context context, String packageName) {
        return getContentProviderDao().getAppBaseInfoByPackageName(context, packageName);
    }

    public static boolean deleteMarketTopInfo(Context context, String packageName) {
        return getContentProviderDao().deleteMarketDesktopInfo(context, packageName);
    }

    /**
     * 保存多桌面数据
     *
     * @param context
     * @param mListAppBaseInfoExit
     * @param dId
     */
    public static void saveDesktopInfo(final Context context, final List<AppBaseInfo> mListAppBaseInfoExit, final String dId) {
        getContentProviderDao().saveDesktopInfo(context, mListAppBaseInfoExit, dId);
    }

    /**
     * 保存流量信息
     *
     * @param context
     * @param trafficStatsInfo
     */
    public static void saveTrafficStatsInfo(final Context context, final TrafficStatsInfo trafficStatsInfo) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveTrafficStats(context, trafficStatsInfo);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 获取流量信息
     *
     * @param context
     * @return
     */
    public static List<TrafficStatsInfo> getTrafficStatsInfos(Context context) {
        return getContentProviderDao().getTrafficStatsInfos(context);
    }

    public static void clearTrafficMonitorInfos(Context context) {
        getContentProviderDao().clearTrafficStatsInfo(context);
    }

    /**
     * 查询当月的流量信息
     *
     * @param context
     * @return
     */
    public static List<TrafficStatsInfo> getMonthTrafficMonitorInfo(Context context, String packageName) {
        return getContentProviderDao().getMonthTrafficMonitorInfos(context, packageName);
    }

    /**
     * 查询当天的流量信息
     *
     * @param context
     * @return
     */
    public static List<TrafficStatsInfo> getDayTrafficMonitorInfos(Context context, String packageName) {
        return getContentProviderDao().getDayTrafficMonitorInfos(context, packageName);
    }

    /**
     * 查询工单是否重复上传
     */
    public static TaskUpInfo getTaskisUp(Context context, String workNo) {
        return getContentProviderDao().getTaskisUp(context, workNo);
    }

    /**
     * 保存待处理上传信息
     *
     * @param context
     * @param info
     */
    public static void saveTaskUpInfo(final Context context, final TaskUpInfo info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveTaskUpInfo(context, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);

    }

    /**
     * 删除待处理上传信息
     *
     * @param context
     * @return
     */
    public static boolean deleteTaskUpInfo(Context context) {
        return getContentProviderDao().deleteTaskUpInfo(context);
    }

    /**
     * 保存工单常量
     *
     * @param context
     * @param info
     */
    public static void saveTaskConstantInfo(final Context context, final String info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveTaskConstantInfo(context, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 删除工单常量信息
     *
     * @param mContext
     */
    public static void deleteTaskConstantInfo(Context mContext) {
        getContentProviderDao().deleteTaskConstantInfo(mContext);
    }

    /**
     * 查询工单常量信息
     *
     * @param mContext
     */
    public static ArrayList<DataBean> getTaskConstantInfo(Context mContext) {
        return getContentProviderDao().getTaskConstantInfo(mContext);
    }

    /**
     * 保存用户行为轨迹
     *
     * @param context
     * @param info
     */
    public static void saveBehaviorTrajectoryInfo(final Context context, final BehaviorTrajectoryInfo info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveBehaviorTrajectoryInfoContext(context, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 删除用户行为轨迹信息
     *
     * @param mContext
     */
    public static void deleteBehaviorTrajectoryInfo(Context mContext, String businessIdentification, String sonBusinessIdentification) {
        getContentProviderDao().deleteBehaviorTrajectoryInfo(mContext, businessIdentification, sonBusinessIdentification);
    }

    /**
     * 更新用户行为轨迹信息
     *
     * @param mContext
     */
    public static boolean updateBehaviorTrajectoryInfo(Context mContext, String id) {
        return getContentProviderDao().updateBehaviorTrajectoryInfo(mContext, id);
    }

    /**
     * 查询用户行为轨迹信息
     *
     * @param mContext
     */
    public static ArrayList<BehaviorTrajectoryInfo> getBehaviorTrajectoryInfo(Context mContext, String businessIdentification, String sonBusinessIdentification) {
        return getContentProviderDao().getBehaviorTrajectoryInfo(mContext, businessIdentification, sonBusinessIdentification);
    }

    /**
     * 保存离线消息
     *
     * @param mContext
     */
    public static void saveMsgOffline(final Context mContext, final MsgOfflineInfo info) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getContentProviderDao().saveMsgOffline(mContext, info);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 获取离线消息
     */
    public static ArrayList<MsgOfflineInfo> getMsgOffline(Context mContext, String topic, String time) {
        return getContentProviderDao().getMsgOffline(mContext, topic, time);
    }
}
